package com.kedacom.emptyproject.util

import com.kedacom.util.LegoLog
import java.util.*

/**
 * description：
 *
 * author: qiuzhiyuan
 * date: 2022/2/24
 * email: qiuzhiyuan@kedacom.com
 */
object DateUtils {
    private const val TAG = "DateUtils"

    /**
     * 获取当前年份
     */
    fun getYear(): Int {
        return Calendar.getInstance().get(Calendar.YEAR)
    }

    /**
     * 获取当前月份
     */
    fun getMonth(): Int {
        return Calendar.getInstance().get(Calendar.MONTH) + 1
    }

    /**
     * 获取当前日期是当前月份的第几天
     */
    fun getCurrentDayOfMonth(): Int {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
    }

    fun getCurrentDayOfWeek(): Int {
        return Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
    }

    /**
     * 获取当前几点钟
     */
    fun getHour(): Int {
        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
    }

    /**
     * 获取当前小时的分钟数
     */
    fun getMinute(): Int {
        return Calendar.getInstance().get(Calendar.MINUTE)
    }

    /**
     * 获取当前分钟的秒数
     */
    fun getSecond(): Int {
        return Calendar.getInstance().get(Calendar.SECOND)
    }

    /**
     * 获取某年某月的第一天是星期几
     */
    fun getFirstDayWeekCurrentMonth(year: Int, month: Int): Int {
        var calendar = Calendar.getInstance()
        calendar.set(year, month - 1, 1)
        //设置该月的第一天是周几
        return calendar[Calendar.DAY_OF_WEEK]
    }

    /**
     * 根据传入的年份和月份 获取当前月份的日历分布
     */
    fun getDayOfMonthFormat(year: Int, month: Int): Array<IntArray> {
        var calendar = Calendar.getInstance()
        calendar.set(year, month - 1, 1)
        // 设置日历格式数组 6行7列（注意：多余的数据需要自己处理）
        var days = Array(6) { IntArray(7) }
        //设置该月的第一天是周几
        val daysOfFirstWeek = calendar[Calendar.DAY_OF_WEEK]
        LegoLog.d(TAG, "daysOfFirstWeek $daysOfFirstWeek")

        //设置本月有多少天
        val daysOfMonth: Int = getDaysOfMonth(year, month)
        LegoLog.d(TAG, "daysOfMonth $daysOfMonth")

        //设置上个月有多少天
        val daysOfLastMonth: Int = getLastDaysOfMonth(year, month)
        LegoLog.d(TAG, "daysOfLastMonth $daysOfLastMonth")

        var dayNum = 1
        var nextDayNum = 1
        //将日期格式填充数组
        for (i in days.indices) {
            for (j in days[i].indices) {
                if (i == 0 && j < daysOfFirstWeek - 1) {
                    days[i][j] = daysOfLastMonth - daysOfFirstWeek + 2 + j
                } else if (dayNum <= daysOfMonth) {
                    days[i][j] = dayNum++
                } else {
                    days[i][j] = nextDayNum++
                }
            }
        }
        return days
    }

    /**
     * 根据传入的年份和月份 判断上一个月有多少天
     */
    fun getLastDaysOfMonth(year: Int, month: Int): Int {
        var lastDayOfMonth: Int
        if (month == 1) {
            lastDayOfMonth = getDaysOfMonth(year - 1, 12)
        } else {
            lastDayOfMonth = getDaysOfMonth(year, month - 1)
        }
        return lastDayOfMonth
    }

    /**
     * 根据传入的年份和月份 判断当前月份有多少天
     */
    fun getDaysOfMonth(year: Int, month: Int): Int {
        when (month) {
            1, 3, 5, 7, 8, 10, 12 -> return 31
            2 -> return if (isLeap(year)) {
                29
            } else {
                28
            }
            4, 6, 9, 11 -> return 30
        }
        return -1
    }

    /**
     * 判断是否是闰年
     */
    fun isLeap(year: Int): Boolean {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true
        }
        return false
    }
}